//
//  JGeocodeSearchModel.m
//  decoration_user
//
//  Created by Mac on 2020/11/17.
//  Copyright © 2020 计海峰. All rights reserved.
//

#import "JGeocodeSearchModel.h"

#import "JLocationInfo.h"

#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

@interface JGeocodeSearchModel ()<AMapSearchDelegate>

@property (nonatomic, strong) AMapSearchAPI *search;

@property (nonatomic, copy) BlockId locationBlock;

@property (nonatomic, copy) BlockId responseBlock;

@end

@implementation JGeocodeSearchModel

JSingletonM();

- (BOOL)checkAddress:(NSString *)address {
    if (!address || address.length <= 0) {
        if (self.errorBlock) {
            NSString *domain = @"";
//            NSString *desc = NSLocalizedString(@"xxx", @"xxx");//NSLocalizedString国际化
            NSString *desc = @"地址不可为空";
            NSDictionary *userInfo = @{NSLocalizedDescriptionKey : desc };
            NSError *error = [NSError errorWithDomain:domain code:-101 userInfo:userInfo];
            self.errorBlock(error);
        }
        return false;
    }
    return true;
}

///  地理编码
/// @param address 地址
/// @param result 结果
- (void)geocodeSearch:(NSString *)address locationInfoResult:(BlockId)result {
    if (![self checkAddress:address]) {
        return;
    }
    self.locationBlock = result;
    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
    geo.address = address;
    [self.search AMapGeocodeSearch:geo];
}

///  地理编码
/// @param address 地址
/// @param result 结果
- (void)geocodeSearch:(NSString *)address geocodeSearchResponseResult:(BlockId)result {
    if (![self checkAddress:address]) {
        return;
    }
    self.responseBlock = result;
    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
    geo.address = address;
    [self.search AMapGeocodeSearch:geo];
}


- (BOOL)checkLatitude:(CGFloat)latitude longitude:(CGFloat)longitude {
    if (latitude <= 0 || longitude <= 0) {
        return false;
    }
    return true;
}

///  逆地理编码
/// @param latitude 经度
/// @param longitude 纬度
- (void)reGeocodeSearchLatitude:(CGFloat)latitude longitude:(CGFloat)longitude locationInfoResult:(BlockId)result; {
    if (![self checkLatitude:latitude longitude:longitude]) {
        return;
    }
    self.locationBlock = result;
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location                    = [AMapGeoPoint locationWithLatitude:latitude longitude:longitude];
    regeo.requireExtension            = false;
    [self.search AMapReGoecodeSearch:regeo];
}

- (void)reGeocodeExtensionSearchLatitude:(CGFloat)latitude longitude:(CGFloat)longitude reGeocodeSearchResponseResult:(BlockId)result {
    if (![self checkLatitude:latitude longitude:longitude]) {
        return;
    }
    self.responseBlock = result;
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location                    = [AMapGeoPoint locationWithLatitude:latitude longitude:longitude];
    regeo.requireExtension            = true;
    [self.search AMapReGoecodeSearch:regeo];
}

//MARK: -  AMapSearchDelegate
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response {
    if (response.geocodes.count == 0) {
        return;
    }
    if (self.locationBlock) {
        AMapGeocode *geocode = response.geocodes.firstObject;
        JLocationInfo *info = [JLocationInfo mj_objectWithKeyValues:geocode];
        info.latitude = geocode.location.latitude;
        info.longitude = geocode.location.longitude;
        info.showName = geocode.formattedAddress;
        self.locationBlock(info);
    }else if (self.responseBlock) {
        self.responseBlock(response);
    }
}
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
    if (response.regeocode != nil) {
        if (self.locationBlock) {
            AMapReGeocode *reGeocode = response.regeocode;
            JLocationInfo *info = [JLocationInfo mj_objectWithKeyValues:reGeocode];
            info.showName = reGeocode.formattedAddress;
            self.locationBlock(info);
        }else if (self.responseBlock) {
            self.responseBlock(response.regeocode);
        }
    }
}


/**
 * @brief 当请求发生错误时，会调用代理的此方法.
 * @param request 发生错误的请求.
 * @param error   返回的错误.
 */
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error; {
    if (self.errorBlock) {
        self.errorBlock(error);
    }
}


//MARK: - lazy

- (AMapSearchAPI *)search {
    if (!_search) {
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

@end
